<?php


namespace App\Http\Controllers\Admin;


use App\Exceptions\LevelWordExistsException;
use App\Models\LevelWord;

class LevelWordController extends Controller
{
    /*
     * @api get /study-api/a/level-words 等级单词
     * @version 0.0.1
     * @group admin
     *
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     * @param query int [level] 等级，默认0
     * @param query string [word] 单词
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $this->validate($this->request, [
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $level = $this->request->input('level');
        $word = $this->request->input('word');
        $query = LevelWord::query();
        if ($level != 0) {
            $query->where('level', $level);
        }
        if ($word) {
            $query->where('en', 'like', "%{$word}%");
        }
        $total = $query->count();
        $levelWordsObj = $query->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get();
        $items = [];
        if ($total) {
            /** @var LevelWord $levelWordObj */
            foreach ($levelWordsObj as $levelWordObj) {
                $items[] = [
                    'id' => $levelWordObj->getId(),
                    'level' => $levelWordObj->getLevel(),
                    'word' => $levelWordObj->getWord(),
                    'chinese' => $levelWordObj->getChinese(),
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }

    /*
     * @api post /study-api/a/level-words 新增等级单词
     * @version 0.0.1
     * @group admin
     *
     * @param body string level 等级
     * @param body string word 单词
     * @param body string chinese 中文意思
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'level' => 'required|integer',
            'word' => 'required|string',
            'chinese' => 'required|string',
        ]);
        $level = $this->request->input('level');
        $word = $this->request->input('word');
        $chinese = $this->request->input('chinese');
        if (LevelWord::where('en', $word)->exists()) {
            throw new LevelWordExistsException();
        }
        /** @var LevelWord $levelWordObj */
        $levelWordObj =  new LevelWord();
        $levelWordObj->setLevel($level);
        $levelWordObj->setWord($word);
        $levelWordObj->setChinese($chinese);
        $levelWordObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /study-api/a/level-words/{wordId} 更新等级单词
     * @version 0.0.1
     * @group admin
     *
     * @param path int wordId 等级单词ID
     * @param body string level 等级
     * @param body string word 单词
     * @param body string chinese 中文意思
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($wordId)
    {
        $this->request->merge(['id' => $wordId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'level' => 'required|integer',
            'word' => 'required|string',
            'chinese' => 'required|string',
        ]);
        $level = $this->request->input('level');
        $word = $this->request->input('word');
        $chinese = $this->request->input('chinese');
        $existsId = LevelWord::where('en', $word)->value('id');
        if ($existsId && $existsId != $wordId) {
            throw new LevelWordExistsException();
        }
        /** @var LevelWord $levelWordObj */
        $levelWordObj =  LevelWord::find($wordId);
        $levelWordObj->setLevel($level);
        $levelWordObj->setWord($word);
        $levelWordObj->setChinese($chinese);
        $levelWordObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /study-api/a/level-words/{wordId} 删除等级单词
     * @version 0.0.1
     * @group admin
     *
     * @param path int templateId 模版ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($wordId)
    {
        LevelWord::destroy(intval($wordId));
        return $this->sendSuccessJson();
    }
}
